<?php

function reprocessDKP($db, $system_id, $verbose=0) {
  
    if($verbose) {
        echo "Beginning DKP reprocessing...<br />";
    }

    if($verbose) {
        echo "Clearing boss and item values...<br />";    
    }

    $sql = "delete from wm_boss_values where boss_value_system_id = '$system_id'";
    $result = $db->query($sql);

    $sql = "delete from wm_item_values where item_system_id = '$system_id'";
    $result = $db->query($sql);

    $sql = "select * from wm_systems where system_id = '$system_id'";
    $result = $db->query($sql);
  
    $system = $db->fetch_assoc($result);

    if($verbose) {
        echo "Initializing constants...<br />";
    }

    $floor_max = 0.5;
    $dkp_start = 25;
    # TODO: move to config
    $off_spec_price = 0;
    
    if($verbose) {
        echo "Creating storage arrays...<br />";
    }

    $dkp = array();
    $dkp_earned = array();
    $dkp_spent = array();
    $dkp_satout = array();

    $item_spent = array(); //item_spent[item] = n
    $item_buys = array(); //item_buys[item] = n

    $boss_spent = array(); //boss_spent[boss] = n
    $boss_credits = array(); // boss_credits[boss] = n

    $chara_credits = array(); //credits for a boss chara_credits[character][boss] = n
    $chara_satout = array(); //credits for a boss chara_satout[character][boss] = n
    $chara_items = array(); //items bought chara_items[character][item] = n
    $chara_off_items = array(); //off spec items bought chara_items[character][item] = n

    if($verbose) {
        echo "Initializing arrays and setting everyone at start DKP value ($dkp_start dkp)...<br />";
    }
  
    $sql = "select * from wm_characters where character_system_id = $system_id and character_approved = 1";
    $result = $db->query($sql);
    while($chara = $db->fetch_assoc($result)) {
        $dkp[$chara["character_id"]] = $dkp_start;
        $dkp_earned[$chara["character_id"]] = 0;
        $dkp_spent[$chara["character_id"]] = 0;
        $dkp_satout[$chara["character_id"]] = 0;

        $chara_credits[$chara["character_id"]] = array();
        $chara_satout[$chara["character_id"]] = array();
        $chara_items[$chara["character_id"]] = array();
        $chara_off_items[$chara["character_id"]] = array();
    }

    if($verbose) {
        echo "Walking through credits and loots...<br />";
    }
  
    $sql = "select * from wm_credits where credit_system_id = $system_id order by credit_date asc";

    $result = $db->query($sql);
    while($credit = $db->fetch_assoc($result)) {
        if($verbose) {
            echo "Examining credit " . $credit["credit_id"] . "...";
            echo "<br />";
        }

        if($verbose) {
            echo "&nbsp;&nbsp;&nbsp;Awarding earned credit to attendees...";
        }
  
        $sql = "select * from wm_attendees where attendee_credit = " . $credit["credit_id"] . " and attendee_system_id = $system_id and attendee_satout = 0";
        $res = $db->query($sql);
        while($att = $db->fetch_assoc($res)) {
            $boss_credits[$credit["credit_boss"]]++;
            $chara_credits[$att["attendee_character"]][$credit["credit_boss"]]++;
        }

        if($verbose) {
            echo " " . $db->num_rows($res) . " credits awarded.<br />";
        }

        if($verbose) {
            echo "&nbsp;&nbsp;&nbsp;Awarding satout credit to satout (" . $credit["credit_boss"] . ")...";
        }

        $sql = "select * from wm_attendees where attendee_credit = " . $credit["credit_id"] . " and attendee_system_id = $system_id and attendee_satout = 1";
    
        $res = $db->query($sql);
        while($att = $db->fetch_assoc($res)) {
            $chara_satout[$att["attendee_character"]][$credit["credit_boss"]]++;
        }

        if($verbose) {
            echo " " . $db->num_rows($res) . " credits awarded. @@<br />";
        }

        $sql = "select wm_loots.* from wm_loots left join item_cache on item_cache.item_id = wm_loots.loot_item where loot_system_id = $system_id and loot_credit = " . $credit["credit_id"] . " order by item_cache.item_name asc";
        $res = $db->query($sql);
    
        while($loot = $db->fetch_assoc($res)) {
            if($verbose) {
                echo "&nbsp;&nbsp;&nbsp;Examining loot " . $loot["loot_id"] . "/" . $loot["loot_item"] . "...";
                print_r($loot);
                echo "<br />";
            }

            $bid_amt = bid2perc($loot["loot_level"]) * $dkp[$loot["loot_character"]];
            if($bid_amt < $floor_max and $loot["loot_level"] > 1) { //$floor_bid[$loot["loot_level"]]) {
                $bid_amt = $floor_max; //$floor_bid[$loot["loot_level"]];
            }
            // Split out off spec loot
            if ($loot["loot_level"] == 1) {
                $chara_off_items[$loot["loot_character"]][$loot["loot_item"]]++;
            } else {
                $chara_items[$loot["loot_character"]][$loot["loot_item"]]++;
            }
            // Only include regular bids, not off spec loot
            if ($loot["loot_level"] > 1) {
                $item_buys[$loot["loot_item"]] ++;
                $item_spent[$loot["loot_item"]] += $bid_amt;
                $boss_spent[$credit["credit_boss"]] += $bid_amt;
            }
            if($verbose) {
                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                echo "Bid $bid_amt";
                echo "<br />";
            }

            $sql = "update wm_loots set loot_bid = '$bid_amt' where loot_id='" . $loot["loot_id"] . "'";
            $ress = $db->query($sql);

            //updating everything
            if($verbose) {
                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                echo "Recalculating dkp...";
                echo "<br />";
            }

            //UPDATE EVERYTHING: START
            //paying for the item
            //$boss_spent[$credit["credit_boss"]] += $paid_amt;
  
            //for everyone, we look at 
            $sql = "select * from wm_characters where character_system_id = $system_id and character_approved = 1";
            $rez = $db->query($sql);
            while($chara = $db->fetch_assoc($rez)) {
                $dkp_earned[$chara["character_id"]] = 0;
                $dkp_spent[$chara["character_id"]] = 0;
        
                // dkp spent on regular loot
                reset($chara_items[$chara["character_id"]]);
                while(list($it, $i) = each($chara_items[$chara["character_id"]])) {
                    if($verbose) {
                        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                        echo '$chara["character_id"]: '.$chara["character_id"].' $it: '.$it.' $i: '.$i;
                        echo "<br />";
                    }
                    $dkp_spent[$chara["character_id"]] += $item_spent[$it]/$item_buys[$it]*$i;
                }
                // dkp spent on off spec loot
                reset($chara_off_items[$chara["character_id"]]);
                while(list($it, $i) = each($chara_off_items[$chara["character_id"]])) {
                    if($verbose) {
                        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                        echo 'off: $chara["character_id"]: '.$chara["character_id"].' $it: '.$it.' $i: '.$i;
                        echo "<br />";
                    }
                    $dkp_spent[$chara["character_id"]] += $off_spec_price*$i;
                }

                reset($chara_credits[$chara["character_id"]]);
                while(list($bs, $i) = each($chara_credits[$chara["character_id"]])) {
                    $dkp_earned[$chara["character_id"]] += $boss_spent[$bs]/$boss_credits[$bs]*$i;
                }


                //@@todo@@ satout
                $dkp[$chara["character_id"]] = $dkp_start + $dkp_earned[$chara["character_id"]] - $dkp_spent[$chara["character_id"]];

                // dkp = dkp_earned - dkp_spent
                // dkp earned = each credit times the value of that credit
                // the value of that credit = dkp spent on that boss
                // dkp spent on that boss = items bought from that boss, summed
                // each item = bids / total
        
            }

            //UPDATE EVERYTHING: END
        }

    }
    /*
     */

    if($verbose) {
        echo "Updating SQL tables...<br />";
    }

    $sql = "select * from wm_characters where character_system_id = $system_id and character_approved = 1";
    $rez = $db->query($sql);
    while($chara = $db->fetch_assoc($rez)) {
        $dkp_satout[$chara["character_id"]] = 0;
        reset($chara_satout[$chara["character_id"]]);

        while(list($bs, $i) = each($chara_satout[$chara["character_id"]])) {
            $dkp_satout[$chara["character_id"]] += $boss_spent[$bs]/$boss_credits[$bs]*$i;
        }


        $cid = $chara["character_id"];
        $adkp = $dkp[$chara["character_id"]];
        $adkp_earned = $dkp_earned[$chara["character_id"]];
        $adkp_spent = $dkp_spent[$chara["character_id"]];
        $adkp_satout = $dkp_satout[$chara["character_id"]];
        $sql = "update wm_characters set character_dkp = '$adkp', character_dkp_earned = '$adkp_earned', character_dkp_spent = '$adkp_spent', character_dkp_satout = '$adkp_satout' where character_id = '$cid' and character_system_id = '$system_id'";
        $res = $db->query($sql);
    }

    //do boss and item values
    while(list($k, $v) = each($boss_spent)) {
        $v = $v/$boss_credits[$k];
        $sql = "insert into wm_boss_values (boss_value_boss, boss_value_value, boss_value_system_id) values ($k, $v, $system_id)";
        $result = $db->query($sql);
    }

    while(list($k, $v) = each($item_spent)) {  
        $v = $v/$item_buys[$k];
        $sql = "insert into wm_item_values (item_id, item_value, item_system_id) values ($k, $v, $system_id)";
        $result = $db->query($sql);
    }

    if($verbose) {
        echo "Reprocessing complete.<br />";
    }
  }

?>